<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Think\Page;
class TaskController extends BaseController
{

    public function add()
    {
        $this->assign('title', '发布任务页面');
        $this->assign('stafflist', D('Staff')->select());
        $relation = D('Station')->relation(true)->select();      
        $this->assign('relationlist', $relation);
      
        $this->assign('monthlist', D('Month')->select());
        $mount = date('m');
        $datetime = date('Y-m-d H:i:s');
        $this->assign('datetime', $datetime);
        $this->assign('nowmonth', $mount);
        $this->assign('url', U('Admin/Task/insert'));
        $this->display('Task_info');
    }

    public function insert()
    {
        foreach (I('post.taskusers') as $v) {
            $where['id'] = $v;
            $staffname = D('Staff')->where($where)->find();
            $data['taskstarttime'] = I('post.taskstarttime');
            $data['taskinfo'] = I('post.taskinfo');
            $data['taskstatus'] = 2;
            $data['staff_id'] = $v;
            $data['staff_name'] = $staffname['name'];
            $data['month_id'] = I('post.month_id');
            $dat['status'] = 1;
           
//             $dat['statusbig'] = I('post.month_id');
           
//修改
            $map['staff_name']=$staffname['name'];
            $map['month']=I('post.month_id');
            $da['staff_name'] = $staffname['name'];
            $da['month'] = I('post.month_id');
            $res=D('Ture')->where($map)->find();
            if ($res['staff_name']==$staffname['name']&&$res['month']==I('post.month_id')){
                D('Ture')->where($map)->save($da);
                D('Task')->add($data);
                D('Staff')->where($where)->save($dat);
                $this->success('发布任务成功！', U('Admin/Task/View'));
            }else{
                D('Ture')->add($da);
    //修改
                if (D('Staff')->where($where)->save($dat)) {
                    if (D('Task')->add($data)) {
                        $this->success('发布任务成功！', U('Admin/Task/View'));
                    } else {
                        $this->error('发布任务失败！');
                    }
                } else {
                    $this->error('发布任务失败！');
                }
            }
            
        }
    }

    public function edit()
    {
        $task = D('Task');
        $where['id'] = I('get.id');
        $tasklist = $task->where($where)->find();
        $map['id'] = $tasklist['staff_id'];
        $taskname = D('Staff');
        $edittaskname = $taskname->field('id')
            ->where($map)
            ->find();
        $this->assign('taskid', $tasklist);
        $this->assign('taskname', $edittaskname);
        $this->assign('title', '计算出差页面');
        $this->assign('url', U('Task/update'));
        $this->display('Task_back');
    }
    
    public function editt()
    {
        $task = D('Task');
        $where['id'] = I('get.id');                     
        $tasklist = $task->where($where)->find();
        $this->assign('taskid', $tasklist);
        $this->assign('title', '编辑页面');
        $this->assign('url', U('Task/updatee'));
        $this->display('Task_editt');
    }
    
   public  function updatee(){
       $task = D('Task');
       $where['id'] = I('post.id');
       $tasklist = $task->where($where)->find();
       $tname['staff_name']= $tasklist['staff_name'];
       $tres=D('Ture')->where($tname)->find();
       

       $post['taskdate']=I('post.taskdate');
       $post['taskadd']=I('post.taskadd');
       

       $exit['taskdate']=I('post.tdate');
       $exit['taskadd']=I('post.tadd');
       
       $daa['taskstarttime']=I('post.taskstarttime');
       $daa['taskendtime']=I('post.taskendtime');
       $daa['taskdate']=$post['taskdate'];
       $daa['taskadd']=$post['taskadd'];
         
       $data['taskdate']=$tres['taskdate']+$post['taskdate']-$exit['taskdate'];
       $data['taskadd']=$tres['taskadd']+$post['taskadd']-$exit['taskadd'];
       $result=D('Task')->where($where)->save($daa);
       if (false !== $result || 0 !== $result){
           if (false !==D('Ture')->where($tname)->save($data) || 0 !==D('Ture')->where($tname)->save($data)){
               $this->success('修改成功！',U('Admin/Task/View'));
           }else {
               $this->error('修改失败！');
           }
       }
       
      
      
   }
    
    

    public function update()
    {
        $task = D('Task');
        $where['id'] = I('post.id');
        $map['id'] = I('post.id1');
        $data['taskstatus'] = 1;
        $data['taskendtime'] = I('post.taskendtime');
        $data['taskbackinfo'] = I('post.taskbackinfo');
        $data['taskadd'] = I('post.taskadd');
        $data['taskdate']=I('post.taskdate');
        $starttime = $task->where($where)->find();
        $wherem['staff_name']=$starttime['staff_name'];
        $wherem['month']=$starttime['month_id'];
        $dat = strtotime(I('post.taskendtime')) - strtotime($starttime['taskstarttime']);
        
//         $b = substr(number_format($dat / (60 * 60 * 24), 1), - 1);
        
//         if ($b < 5) {
//             $b = 0;
//         } else {
//             $b = 5;
//         }
//         $c = substr(number_format($dat / (60 * 60 * 24), 1), 0, - 2);
//         $data['taskdate'] = floatval($c . '.' . $b)+1;
        $result = $task->where($where)->save($data);
         
        $ture=D('Ture')->where($wherem)->find(); //修改
      
      
        if ($ture['staff_name']==$starttime['staff_name']&&$ture['month']==$starttime['month_id']){
            $sa['taskadd'] = I('post.taskadd')+ $ture['taskadd'];
            $sa['taskdate']=I('post.taskdate')+$ture['taskdate'];
            D('Ture')->where($wherem)->save($sa);
           
            $staffcha = D('Staff')->where($map)->find();
            $daa['status'] = 0;
            $daa['addday'] = I('post.taskadd') + $staffcha['addday'];
            $daa['travelday'] = $data['taskdate'] + $staffcha['travelday'];
            
            $staffstatus = D('Staff')->where($map)->save($daa);
            if ($dat <= 0) {
                $this->error('回来时间不能小于开始时间！');
            } else {
                if ($staffstatus) {
                   
                        $this->success('提交成功！', U('Admin/Task/View'));
                   
                } else {
                    $this->error('提交失败！');
                }
            }
        }else {
            $sa['taskadd'] = I('post.taskadd');
            $sa['taskdate']=I('post.taskdate');
            D('Ture')->where($wherem)->save($sa);//修改
            
            $staffcha = D('Staff')->where($map)->find();
            $daa['status'] = 0;
            $daa['addday'] = I('post.taskadd') + $staffcha['addday'];
            $daa['travelday'] = $data['taskdate'] + $staffcha['travelday'];
            
            $staffstatus = D('Staff')->where($map)->save($daa);
            if ($dat <= 0) {
                $this->error('回来时间不能小于开始时间！');
            } else {
                if ($staffstatus) {                  
                    $this->success('提交成功！', U('Admin/Task/View'));                   
                } else {
                    $this->error('提交失败！');
                }
            }
            
      }
        
       
    }

    public function delete()
    {
        $map['id'] = $_GET['id'];
        $staffid = D('Task')->where($map)->find();
        $where['id'] = $staffid['staff_id'];
        $da['status'] = 0;
        $result = D('Staff')->where($where)->save($da);
        if (D('Task')->gettaskid()) {
            // if($result){
            $data['state'] = 1;
            $data['msg'] = '删除成功';
            $this->ajaxReturn($data);
            // }else {
            // $data['state']=0;
            // $data['msg']='删除失败';
            // $this->ajaxReturn($data);
            // }
        } else {
            $data['state'] = 0;
            $data['msg'] = '删除失败';
            $this->ajaxReturn($data);
        }
    }

    public function View()
    {
        $year = date('Y');
        $b = $_GET['tex'];        
        $zl=0;
        $zr=0;              
        if ($b == null) {           
            $b= date("m");
            $t = date("t");           
            
        } else {
            if ($b == 9 || $b == 4 || $b == 6 || $b == 11) {
                $t = 30;
            } else if ($b == 1 || $b == 3 || $b == 5 || $b == 7 || $b == 8 || $b == 10 || $b == 12) {
                    $t = 31;
                } else if ($b == 2) {
                        if (($year % 4 == 0 && $year % 100 != 0) || ($year % 100 == 0 && $year % 400 == 0)) {
                            $t = 29;
                        } else {
                            $t = 28;
                        }
                    }
        }
        
        $days=$t;
        for ($i=1; $i<=$days; $i++) {
            $day = $year.'-'.$b.'-'.$i;
            $w = date('w',strtotime($day));
            if ($w == 6) {
                $zl+=1;
            }else if($w == 0){
                $zr+=1;
            }
        }
        
        $gxday=0.5*$zl+$zr;
        $resday=$t-$gxday;
        
        $this->assign('t', $resday);
        $a = $_GET['tex'];
        if ($a == null) {
            $a = date('m');
            if ($a < 10) {
                $a = substr($a, 1);
            }
        } else {
            $a = $_GET['tex'];
        }
        $this->assign('month', $a);
        $this->assign('null', null);
        $this->assign('title', '任务列表页面');
        $this->assign('info', '添加出差人');
        
        $search = '%' . htmlspecialchars(I('get.search')) . '%';
        $where['staff_name'] = array(
            'like',
            $search
        );
        if ($search !== null) {
            $where['staff_name'] = array(
                'like',
                $search
            );
        }
            
        $where['month_id'] = $a;
        $ss = D('Task');
        $count = $ss->where($where)->count();
        $page = new Page($count, 20);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->rollPage = '6';
        $page->setConfig('theme', '%HEADER%  共    %TOTAL_PAGE% 页   %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        
        $tasklist = D('Task')->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order('convert(staff_name using utf8) desc')
            ->select();
       
           
            
        $this->assign('page', $show);
        $this->assign('tasklist', $tasklist);
        $year = date('Y');
        $this->assign('year', $year);
        $this->assign('monthlist', D('Month')->select());
        $this->assign('url', U('Task/search'));
        $this->display('Task_list');
    }



    public function copy()
    {
        $copyid = $_POST['select'];
        $map['id'] = array(
            'in',
            $copyid
        );
        $map['taskstatus'] = 1;   
        
        $where['id'] = array(
            'in',
            $copyid
        );
        $where['taskstatus'] = 2;
                
//         $data['status'] = 0;
       
//         $result = D('Task')->where($map)->delete();
      
//         D('Staff')->where($map)->save($data);
        $reconut=D('Task')->where($where)->select();
     if ($reconut=='') {
         if (D('Task')->where($map)->delete()){
             $data['state'] = 1;
             $data['msg'] = '删除成功';
             $this->ajaxReturn($data);
         }
            } else {
                $data['state'] = 0;
                $data['msg'] = '还有人没有结算完毕,无法批量删除';
                $this->ajaxReturn($data);
            }
    }

    public function montha()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['d'] = $_GET['tex'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }

    public function search()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['id'] = $_GET['tex'];
            $data['search'] = $_GET['search'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }





    
   
      
    
}








